package com.nbot.server.controller;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nbot.server.domain.ViewChklogCountDO;
import com.nbot.server.service.ViewChklogCountService;
import com.nbot.server.util.PageUtils;
import com.nbot.server.util.Query;
import com.nbot.server.util.R;
import com.nbot.server.annotation.Log;

/**
 * VIEW
 *
 * @author nbot
 * @email nbot@163.com
 * @date 2018-06-23 12:12:05
 */

@Controller
@RequestMapping("/server/viewChklogCount")
public class ViewChklogCountController {
    @Autowired
    private ViewChklogCountService viewChklogCountService;

    @GetMapping()
    @RequiresPermissions("server:viewChklogCount:viewChklogCount")
    String ViewChklogCount() {
        return "server/viewChklogCount/viewChklogCount" ;
    }

    @ResponseBody
    @Log("查看VIEW")
    @GetMapping("/list")
    @RequiresPermissions("server:viewChklogCount:viewChklogCount")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        Map<String,Object> mapTemp = new HashMap<String,Object>();
        CovertMapTime(query, mapTemp);
        /*
        String schType = "0";
        if(mapTemp.get("schType")!=null)
            schType= mapTemp.get("schType").toString();
            */

        List<ViewChklogCountDO> viewChklogCountList;
        int total;
        /*
        if(schType.equals("1"))
        {
            viewChklogCountList = viewChklogCountService.listTag(mapTemp);
            total = viewChklogCountService.countTag(mapTemp);
        }else if(schType.equals("2"))
        {
            viewChklogCountList = viewChklogCountService.listCls(mapTemp);
            total = viewChklogCountService.countCls(mapTemp);
        }
        else if(schType.equals("3"))
        {
            viewChklogCountList = viewChklogCountService.listLev(mapTemp);
            total = viewChklogCountService.countLev(mapTemp);
        }else
        {
            viewChklogCountList = viewChklogCountService.listWeizhi(mapTemp);
            total = viewChklogCountService.countWeizhi(mapTemp);
        }
        */

        viewChklogCountList = viewChklogCountService.listLev(mapTemp);
        total = viewChklogCountService.countLev(mapTemp);

        PageUtils pageUtils = new PageUtils(viewChklogCountList, total);
        return pageUtils;
    }

    static void CovertMapTime(Query query, Map<String, Object> mapTemp) {
        for (Map.Entry<String, Object> entry : query.entrySet()) {
            //Map.entry<Integer,String> 映射项（键-值对）  有几个方法：用上面的名字entry
            //entry.getKey() ;entry.getValue(); entry.setValue();
            //map.entrySet()  返回此映射中包含的映射关系的 Set视图。
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
            if((entry.getKey().toString().equals("checktimebegin")
                    ||entry.getKey().toString().equals("checktimeend")) && entry.getValue()!= null )
            {
                mapTemp.put(entry.getKey(),Long.parseLong(entry.getValue().toString()));
                System.out.println("Long!! key= " + entry.getKey() + " and value= " + entry.getValue()
                        +" long value= "+Long.parseLong(entry.getValue().toString()));
            }
            else if(entry.getKey().toString().equals("chktimebeginstr")
                    ||entry.getKey().toString().equals("chktimeendstr") )
            {
                SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                mapTemp.put(entry.getKey(),Timestamp.valueOf(entry.getValue().toString()));
                System.out.println("Data!! key= " + entry.getKey() + " and value= " + entry.getValue()
                        +" datesdf value= "+ Timestamp.valueOf(entry.getValue().toString()));
            }else
            {
                mapTemp.put(entry.getKey(),entry.getValue());
            }
        }
    }

    @GetMapping("/add")
    @RequiresPermissions("server:viewChklogCount:add")
    String add() {
        return "server/viewChklogCount/add" ;
    }

    @GetMapping("/edit/{checkatid}")
    @RequiresPermissions("server:viewChklogCount:edit")
    String edit(@PathVariable("checkatid") Long checkatid, Model model) {
            ViewChklogCountDO viewChklogCount = viewChklogCountService.get(checkatid);
        model.addAttribute("viewChklogCount" , viewChklogCount);
        return "server/viewChklogCount/edit" ;
    }

    /**
     * 保存
     */
    @Log("保存VIEW")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("server:viewChklogCount:add")
    public R save(ViewChklogCountDO viewChklogCount) {
        if (viewChklogCountService.save(viewChklogCount) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @Log("修改VIEW")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("server:viewChklogCount:edit")
    public R update(ViewChklogCountDO viewChklogCount) {
            viewChklogCountService.update(viewChklogCount);
        return R.ok();
    }

    /**
     * 删除
     */
    @Log("删除VIEW")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("server:viewChklogCount:remove")
    public R remove( Long checkatid) {
        if (viewChklogCountService.remove(checkatid) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @Log("批量删除VIEW")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("server:viewChklogCount:batchRemove")
    public R remove(@RequestParam("ids[]") Long[]checkatids) {
            viewChklogCountService.batchRemove(checkatids);
        return R.ok();
    }
    /**
     * tag
     */
    @GetMapping( "/zhanHao")
    @ResponseBody
    public List<ViewChklogCountDO> listZhanhao(){
        return viewChklogCountService.listZhanhao();
    }
}
